package com.example.administrator.foodrowfresh.configs.classifyconfing.classifyadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import com.example.administrator.foodrowfresh.R;
import com.example.administrator.foodrowfresh.configs.classifyconfing.classifyentity.MyVegetableEntity;
import com.example.administrator.foodrowfresh.superBase.MyBaseAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lqr on 2016/5/2.
 */
public class MyRightAdapter extends MyBaseAdapter<MyVegetableEntity.DataBean.ListBean> {
    private List<MyVegetableEntity.DataBean.ListBean> list;
    private Context mContext;
    private AdapterView.OnItemClickListener listener;
    public MyRightAdapter(List<MyVegetableEntity.DataBean.ListBean> list, Context mContext) {
        super(list, mContext);
        this.list=list;
        this.mContext=mContext;
    }
    //给gridview设置点击事件
    public void setListener(AdapterView.OnItemClickListener listener){
        this.listener=listener;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(R.layout.layout_right_item,parent,false);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
          holder= (ViewHolder) convertView.getTag();
        }
        holder.right_text.setText(list.get(position).getMetaKeywords());
        List<MyVegetableEntity.DataBean.ListBean.SmallCategoriesBean> gridPath = list.get(position).getSmallCategories();
        //创建一个list来储存gridview的数据：

        List<Map<String,String>> gridList=new ArrayList<>();
        for (int i = 0; i <gridPath.size(); i++) {
            String sCategoryName = gridPath.get(i).getSCategoryName();
            String pictureUrl = gridPath.get(i).getPictureUrl();
            int sCategoryId = gridPath.get(i).getSCategoryId();
            Map<String,String> map=new HashMap<>();
            map.put("sCategoryName",sCategoryName);
            map.put("pictureUrl",pictureUrl);
            map.put("sCategoryId",sCategoryId+"");
            gridList.add(map);
        }

        //给gridview设置适配器
        MyGridViewAdapter myGridViewAdapter=new MyGridViewAdapter(gridList,mContext);
        holder.right_grid.setAdapter(myGridViewAdapter);
        holder.right_grid.setOnItemClickListener(listener);
        AdapterGridView.setGridViewHeightBasedOnChildren(holder.right_grid);
        return convertView;
    }

    public class ViewHolder{
        TextView right_text;
        GridView right_grid;

        public ViewHolder(View view) {
            right_text= (TextView) view.findViewById(R.id.right_item);
            right_grid= (GridView) view.findViewById(R.id.right_gridview);
        }
    }
}
